home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 April: Mac OS SDK / Dev.CD Apr 00 SDK1.toast / Development Kits / Mac OS / Open Transport 1.3 / Open Transport SDK / Open Tpt Client Developer / Samples / Internet / OTTcpPitchByNameSample.cp < prev    next >
Encoding:
Text File  |  1998-04-30  |  10.2 KB  |  450 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        OTTcpPitchByNameSample.cp
  3.  
  4.     Contains:    Tcp pitch sample.
  5.  
  6.     Copyright:    © 1993-1997 by Apple Computer, Inc., all rights reserved.
  7.  
  8. */
  9.  
  10.  
  11. // OT TCP Pitch Test Program Connecting By Name Rather Than By Address (as an SIOW app)
  12.  
  13. #include <QuickDraw.h>
  14. #include <stdio.h>
  15. #include <StdLib.h>
  16. #include <TextUtils.h>
  17. #include <strings.h>
  18. #include <String.h>
  19. #include <Events.h>
  20. #include <Menus.h>
  21. #include <Devices.h>
  22. #include <OpenTransport.h>
  23. #include <OpenTptInternet.h>
  24.  
  25. /*******************************************************************************
  26. ** GLOBAL VARIABLES
  27. ********************************************************************************/
  28.  
  29. #define kMaxDataLen 256
  30.  
  31. const size_t    kMyPoolSize = 60000;
  32.  
  33. InetPort      gPitchPort        = 0;
  34. InetHost    gPitchIpAddr    = 0;
  35.  
  36. unsigned short gBindCompleted        = 0;
  37. unsigned short gCallRcvOrdDiscon    = 0;
  38.  
  39. char theAddr[257];
  40. DNSAddress* hisAddr = (DNSAddress*)theAddr;
  41.  
  42.  
  43. struct InetAddress sndsin, rcvsin, reqsin, retsin;
  44. char data[kMaxDataLen];
  45. char defaultData[] = "Go Cal, beat Stanford !!!";
  46.  
  47. /*******************************************************************************
  48. ** Function Prototypes
  49. ********************************************************************************/
  50.  
  51. void Inits();
  52. void CleanUp();
  53. void Idle();    
  54. void DoIt();
  55.  
  56.  
  57. /*******************************************************************************
  58. **  main function
  59. ********************************************************************************/
  60.  
  61. void main()
  62. {
  63.     Inits();
  64.     DoIt();
  65.     CleanUp();
  66. }
  67.  
  68. /*******************************************************************************
  69. ** Initialize Quickdraw and ASLM
  70. ********************************************************************************/
  71.  
  72. void Inits()
  73. {
  74.     InitGraf(&qd.thePort);
  75.     if (InitOpenTransport() != kOTNoError)
  76.     {
  77.         fprintf(stderr, "OTTcpPitch: Could not initialize ASLM, exiting\n");
  78.         exit(1);
  79.     }
  80. }
  81.  
  82. /*******************************************************************************
  83. ** ShowEndpointOptions
  84. ********************************************************************************/
  85. void ShowEndpointOptions(TEndpoint*    ep)
  86. {
  87.     /*
  88.      *    This function retrieves and displays 
  89.      *    the IP and TCPP endpoint options for this endpoint.
  90.      */
  91.      
  92.     OSStatus    err;
  93.     TOptMgmt*            ret = (TOptMgmt*)OTAlloc(ep, T_OPTMGMT, T_OPT, &err);
  94.  
  95.     
  96.     do
  97.     {
  98.         fprintf(stderr, "Current Readable IP Option Settings for endpoint @ %08lX:\n", ep);
  99.         if ( ret == NULL )
  100.         {
  101.             fprintf(stderr, "ERROR: could not allocate TOptMgmt structure (%d)\n", err);
  102.             break;
  103.         }
  104.         //
  105.         // Get the current IP options
  106.         //
  107.         TOptMgmt        req;
  108.         TOptionHeader    option;
  109.         
  110.         option.len        = kOTOptionHeaderSize;
  111.         option.level    = INET_IP;
  112.         option.name        = T_ALLOPT;
  113.         
  114.         req.opt.buf = (UInt8*)&option;
  115.         req.opt.len    = kOTOptionHeaderSize;
  116.         req.flags    = T_CURRENT;
  117.         
  118.         err = ep->OptionManagement(&req, ret);
  119.         if ( err != kOTNoError )
  120.         {
  121.             fprintf(stderr, "ERROR: OptionManagement T_CURRENT request returned %d\n", err);
  122.             break;
  123.         }
  124.         //
  125.         // Now, let's print the options
  126.         //
  127.         {
  128.             TOption*    opt = (TOption*)ret->opt.buf;
  129.             char        string[512];
  130.  
  131.             err = OTCreateOptionString("ip", &opt, ret->opt.buf + ret->opt.len,
  132.                                        string, sizeof(string));
  133.             
  134.             if ( err == kOTNoError )
  135.             {
  136.                 char*    str = string;
  137.                 size_t    len = 0;
  138.                 while ( true )
  139.                 {
  140.                     char* temp = strchr(str, ',');
  141.                     if ( temp == NULL )
  142.                     {
  143.                         fprintf(stderr, "%s\n", str);
  144.                         break;
  145.                     }
  146.                     if ( len + temp - str + 1 > 80 )
  147.                     {
  148.                         fprintf(stderr, "\n");
  149.                         if ( *str == ' ' )
  150.                             str += 1;
  151.                         len = 0;
  152.                     }
  153.                     fprintf(stderr, "%*.*s", temp - str + 1, temp - str + 1, str);
  154.                     len += temp - str + 1;
  155.                     str = temp + 1;
  156.                 }
  157.             }
  158.         }
  159.         
  160.         TOption* opt =  OTFindOption(ret->opt.buf, ret->opt.len, INET_IP, IP_TTL);
  161.             
  162.         if ( opt == NULL )
  163.             fprintf(stderr, "ERROR:OptionManagement did not have IP_TTL in returned options\n");
  164.  
  165.         //
  166.         // Get the current TCP options
  167.         //
  168.         fprintf(stderr, "Current Readable TCP Option Settings for endpoint @ %08lX:\n", ep);
  169.         option.len        = kOTOptionHeaderSize;
  170.         option.level    = INET_TCP;
  171.         option.name        = T_ALLOPT;
  172.         
  173.         req.opt.buf = (UInt8*)&option;
  174.         req.opt.len    = kOTOptionHeaderSize;
  175.         req.flags    = T_CURRENT;
  176.         
  177.         err = ep->OptionManagement(&req, ret);
  178.         if ( err != kOTNoError )
  179.         {
  180.             fprintf(stderr, "ERROR: OptionManagement T_CURRENT request returned %d\n", err);
  181.             break;
  182.         }
  183.         //
  184.         // Now, let's print the options
  185.         //
  186.         {
  187.             TOption*    opt = (TOption*)ret->opt.buf;
  188.             char        string[512];
  189.  
  190.             err = OTCreateOptionString("tcp", &opt, ret->opt.buf + ret->opt.len,
  191.                                        string, sizeof(string));
  192.             
  193.             if ( err == kOTNoError )
  194.             {
  195.                 char*    str = string;
  196.                 size_t    len = 0;
  197.                 while ( true )
  198.                 {
  199.                     char* temp = strchr(str, ',');
  200.                     if ( temp == NULL )
  201.                     {
  202.                         fprintf(stderr, "%s\n", str);
  203.                         break;
  204.                     }
  205.                     if ( len + temp - str + 1 > 80 )
  206.                     {
  207.                         fprintf(stderr, "\n");
  208.                         if ( *str == ' ' )
  209.                             str += 1;
  210.                         len = 0;
  211.                     }
  212.                     fprintf(stderr, "%*.*s", temp - str + 1, temp - str + 1, str);
  213.                     len += temp - str + 1;
  214.                     str = temp + 1;
  215.                 }
  216.             }
  217.             fprintf(stderr, "\n");
  218.         }
  219.         
  220.     } while ( false );
  221.     OTFree(ret, T_OPTMGMT);
  222. }
  223.  
  224.  
  225. /*******************************************************************************
  226. ** Clean up at the end
  227. ********************************************************************************/
  228.  
  229. void CleanUp()
  230. {
  231.     CloseOpenTransport();
  232. }
  233.  
  234. /*******************************************************************************
  235. ** Idle
  236. ********************************************************************************/
  237.  
  238. void Idle()
  239. {
  240.     SystemTask();
  241. }
  242.  
  243. /*******************************************************************************
  244. ** EventHandler
  245. ********************************************************************************/
  246.  
  247. pascal void EventHandler(void*, OTEventCode event, OTResult /* result */, void*)
  248. {
  249.  
  250.     switch ( event )
  251.     {
  252.         case T_BINDCOMPLETE:
  253.                             gBindCompleted = 1;
  254.                             break;
  255.         case T_ORDREL:
  256.                             gCallRcvOrdDiscon = 1;
  257.                             break;
  258.         default:
  259.                             DebugStr("\pEventHandler got unexpected event");
  260.                             break;
  261.     }
  262.     return;
  263. }
  264.  
  265. /*******************************************************************************
  266. ** DoIt
  267. ********************************************************************************/
  268.  
  269. void DoIt()
  270. {
  271.     TEndpoint*        ep = NULL;
  272.     TEndpointInfo    info;
  273.     TBind            req, ret;
  274.     TCall            sndcall, rcvcall;
  275.     OSStatus        err = kOTNoError;
  276.     long            myport = 0;
  277.     InetHost        myaddr = 0;
  278.     char            mystr[255];
  279.     long            bytes = 0;
  280.     OTFlags            flags = 0;
  281.  
  282.     myport = 0;
  283.     fprintf(stderr, "Enter local TCP port number\n");
  284.     if (gets(mystr) != 0) 
  285.     {
  286.         stringtonum(mystr, &myport);
  287.         gPitchPort =(InetPort)  myport;
  288.     }
  289.     myaddr = 0;
  290.     fprintf(stderr, "Enter local IP address\n");
  291.     if (gets(mystr) != 0)
  292.     {
  293.         if (OTInetStringToHost(mystr, &myaddr) == 0)
  294.         {
  295.             gPitchIpAddr = (InetHost) myaddr;
  296.         }
  297.     }
  298.     fprintf(stderr, "Enter the target as name:port\n");
  299.     if ( gets(hisAddr->fName) != 0 )
  300.     {
  301.         hisAddr->fAddressType = kOTGenericName;
  302.     }
  303.  
  304.     fprintf(stderr, "What should I send ? (enter data string)\n");
  305.     if ( gets(data) == 0 )
  306.     {
  307.         strcpy(data, defaultData);
  308.         fprintf(stderr, "sending default data: <%s>\n", data);
  309.     }
  310.     data[strlen(data)] = '\n';
  311.  
  312.     fprintf(stderr, "The program will send a packet to <%s> \n", hisAddr->fName);
  313.  
  314.     memset(&sndsin, 0, sizeof(struct InetAddress));
  315.     memset(&rcvsin, 0, sizeof(struct InetAddress));
  316.     memset(&sndcall, 0, sizeof(TCall));
  317.     memset(&rcvcall, 0, sizeof(TCall));
  318.     memset(&reqsin, 0, sizeof(struct InetAddress));
  319.     memset(&retsin, 0, sizeof(struct InetAddress));
  320.     memset(&req, 0, sizeof(TBind));
  321.     memset(&ret, 0, sizeof(TBind));
  322.  
  323.     do
  324.     {
  325.         //
  326.         // Now create a TCP
  327.         //
  328.         ep = OTOpenEndpoint(OTCreateConfiguration(kTCPName), 0, &info, &err);
  329.  
  330.         if ( ep == NULL || err != kOTNoError )
  331.         {
  332.             ep = NULL;
  333.             fprintf(stderr,"ERROR: OpenEndpoint(\"TCP\") failed with %d\n", err);
  334.             break;
  335.         }
  336.  
  337.         err = ep->SetSynchronous();
  338.         if ( err != kOTNoError )
  339.         {
  340.             fprintf(stderr, "ERROR: SetSynchronous() failed with %d\n", err);
  341.             break;
  342.         }
  343.  
  344.         //
  345.         // Install notifier we're going to use for testing
  346.         //
  347.         err = ep->InstallNotifier(&EventHandler, 0);
  348.         if ( err != kOTNoError )
  349.         {
  350.             fprintf(stderr, "ERROR: InstallNotifier() failed with %d\n", err);
  351.             break;
  352.         }
  353.  
  354.         //
  355.         // Try to bind
  356.         // 
  357.         OTInitInetAddress(&reqsin, gPitchPort, gPitchIpAddr);
  358.  
  359.         req.addr.len = sizeof(struct InetAddress);
  360.         req.addr.buf = (unsigned char *) &reqsin;
  361.         req.qlen = 2;                                        // don't care for tcp
  362.         ret.addr.maxlen = sizeof(struct InetAddress);
  363.         ret.addr.buf = (unsigned char *) &retsin;
  364.  
  365.         // bind TCP to current address and port
  366.         err = ep->Bind(&req, &ret);
  367.         if ( err != kOTNoError )
  368.         {
  369.             fprintf(stderr, "ERROR: Bind() failed with %d\n", err);
  370.             break;
  371.         }
  372.         err = ep->SetSynchronous();
  373.         if ( err != kOTNoError )
  374.         {
  375.             fprintf(stderr, "ERROR: SetSynchronous() failed with %d\n", err);
  376.             break;
  377.         }
  378.         
  379.         ShowEndpointOptions(ep);
  380.  
  381.         sndcall.addr.len = sizeof(OTAddressType) + strlen(hisAddr->fName);
  382.         sndcall.addr.buf = (UInt8*)hisAddr;
  383.     
  384.         rcvcall.addr.maxlen = sizeof(struct InetAddress);
  385.         rcvcall.addr.buf = (unsigned char *) &rcvsin;
  386.  
  387.         err = ep->Connect(&sndcall, &rcvcall);
  388.         if ( err != kOTNoError )
  389.         {
  390.             fprintf(stderr, "ERROR: Connect() failed with %d\n", err);
  391.             break;
  392.         }
  393.  
  394.         bytes = ep->Snd(data, strlen(data), flags);
  395.         if ( bytes >= 0 )
  396.         {
  397.             fprintf(stderr, "Sent packet\nbytes: <%d> data: %s", bytes, data);
  398.         }
  399.         else 
  400.         {
  401.             fprintf(stderr, "ERROR: Snd() failed with %d\n", bytes);
  402.         }
  403.         Idle();
  404.     } while (false);
  405.  
  406.     if ( ep != NULL )
  407.     {
  408.         err = ep->SndOrderlyDisconnect();
  409.         if ( err != kOTNoError )
  410.         {
  411.             if ( err == kOTLookErr )
  412.                 fprintf(stderr, "SndOrderlyDisconnect() returns %d\n", err);
  413.             else
  414.                 fprintf(stderr, "ERROR: SndOrderlyDisconnect() failed with %d\n", err);
  415.         }
  416.     
  417.         while (gCallRcvOrdDiscon == 0)
  418.             Idle();
  419.         err = ep->RcvOrderlyDisconnect();
  420.         if ( err != kOTNoError )
  421.         {
  422.             fprintf(stderr, "ERROR: RcvOrderlyDisconnect() failed with %d\n", err);
  423.         }
  424.     
  425.         //
  426.         // Remove notifier
  427.         //
  428.         ep->RemoveNotifier();
  429.         //
  430.         // Try to Unbind
  431.         // 
  432.     /*
  433.         err = ep->Unbind();
  434.         if ( err != kOTNoError )
  435.         {
  436.             fprintf(stderr, "ERROR: Unbind() returned %d\n", err);
  437.         }
  438.     */
  439.         //
  440.         // Get rid of endpoint.
  441.         //
  442.         err = OTCloseProvider(ep);
  443.         if ( err != kOTNoError )
  444.         {
  445.             fprintf(stderr, "ERROR: CloseEndpoint() failed with %d\n", err);
  446.         }
  447.     }
  448.     fprintf(stderr, "Bye\n");
  449. }
  450.